import React, {useEffect, useState} from 'react';
import {Text, View} from "@tarojs/components";

import Taro from "@tarojs/taro";

import './index.scss';

type WifiProps = {};

type WifiState = {
  startError: string;
  wifiListError: boolean;
  wifiListErrorInfo: string;
  system: string | number;
  platform: 'android' | 'ios';
  ssid: string;
  pass: string;
  bssid: string;
  endError: string;
};

const Index: React.FC<WifiProps> = () => {
  const [state, setState] = useState<WifiState>({
    startError: '',
    wifiListError: false,
    wifiListErrorInfo: '',
    system: '',
    platform: '',
    ssid: 'wifi帐号',
    pass: 'wifi密码',
    bssid: '',
    endError: ''
  });

  useEffect(() => {
    onLoad();
  }, []);

  const onLoad = () => {
    Taro.getSystemInfo({
      success: (res: WechatMiniprogram.GetSystemInfoSuccessCallbackResult) => {
        let system: number | undefined;
        switch (res.platform) {
          case 'android':
            system = parseInt(res.system.substr(8));
            break;
          case 'ios':
            system = parseInt(res.system.substr(4));
            break;
        }

        if ((res.platform === 'android' && system && system < 6) ||
          (res.platform === 'ios' && system && system < 11)) {
          setState(prevState => ({...prevState, startError: '手机版本暂时不支持'}));
          return;
        }

        setState(prevState => ({...prevState, platform: res.platform}));
        startWifi();
      }
    });
  };

  const startWifi = () => {
    Taro.startWifi({
      success: () => {
        getList();
      },
      fail: (res: WechatMiniprogram.GeneralCallbackResult) => {
        setState(prevState => ({...prevState, startError: res.errMsg}));
      }
    });
  };

  const getList = () => {
    if (state.platform === 'android') {
      Taro.getWifiList({
        success: (res: WechatMiniprogram.GetWifiListSuccessCallbackResult) => {
          androidList();
        },
        fail: (res: WechatMiniprogram.GeneralCallbackResult) => {
          setState(prevState => ({
            ...prevState,
            wifiListError: true,
            wifiListErrorInfo: res.errMsg
          }));
        }
      });
    } else if (state.platform === 'ios') {
      iosList();
    }
  };

  const androidList = () => {
    Taro.onGetWifiList((res: WechatMiniprogram.OnGetWifiListCallbackResult) => {
      if (res.wifiList?.length) {
        let signalStrength = 0;
        let bssid = '';
        for (const wifi of res.wifiList) {
          if (wifi.SSID === state.ssid && wifi.signalStrength > signalStrength) {
            bssid = wifi.BSSID;
            signalStrength = wifi.signalStrength;
          }
        }

        if (!signalStrength) {
          setState(prevState => ({
            ...prevState,
            wifiListError: true,
            wifiListErrorInfo: '未查询到设置的wifi'
          }));
          return;
        }

        setState(prevState => ({...prevState, bssid}));
        connected();
      } else {
        setState(prevState => ({
          ...prevState,
          wifiListError: true,
          wifiListErrorInfo: '未查询到设置的wifi'
        }));
      }
    });
  };

  const iosList = () => {
    setState(prevState => ({
      ...prevState,
      wifiListError: true,
      wifiListErrorInfo: 'IOS暂不支持'
    }));
  };

  const connected = () => {
    Taro.connectWifi({
      SSID: state.ssid,
      BSSID: state.bssid,
      password: state.pass,
      success: () => {
        setState(prevState => ({...prevState, endError: 'wifi连接成功'}));
      },
      fail: (res: WechatMiniprogram.GeneralCallbackResult) => {
        setState(prevState => ({...prevState, endError: res.errMsg}));
      }
    });
  };

  return (
    <View>
      <View>
        <View className='error'>
          {/*<Icon type="warn" size="40"/>*/}
          <Text>{state.startError}</Text>
        </View>
        <View className='info'>请尝试手动连接。</View>
        <View className='info'>
          <Text>wifi帐号:</Text>
          <Text>{state.ssid}</Text>
        </View>
        <View className='info'>
          <Text>wifi密码:</Text>
          <Text selectable className='pass'>{state.pass}</Text>
        </View>
      </View>

      <View>
        <View className='error'>
          {/*<Icon type="warn" size="40"/>*/}
          <Text>{state.wifiListErrorInfo}</Text>
        </View>
        <View className='info'>请尝试手动连接。</View>
        <View className='info'>
          <Text>wifi帐号:</Text>
          <Text>{state.ssid}</Text>
        </View>
        <View className='info'>
          <Text>wifi密码:</Text>
          <Text selectable className='pass'>{state.pass}</Text>
        </View>
      </View>

      <View>
        <View className='success'>
          {/*<Icon type="success" size="40"/>*/}
          <Text>{state.endError}</Text>
        </View>
      </View>

    </View>
  )
};

export default Index;
